package com.xin.di.uav.applet.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.xin.di.uav.applet.mapper.RoleMapper;
import com.xin.di.uav.applet.mapper.UserAccountMapper;
import com.xin.di.uav.applet.po.UserAccountPO;
import com.xin.di.uav.applet.request.UserAccountsAddParam;
import com.xin.di.uav.applet.request.UserAccountsEditParam;
import com.xin.di.uav.applet.resp.UserAccountResp;
import com.xin.di.uav.applet.service.UserAccountService;
import com.xin.di.uav.common.awt.JwtUtil;
import com.xin.di.uav.common.exception.BusinessException;
import com.xin.di.uav.common.request.common.IdRequest;
import com.xin.di.uav.common.utils.BeanUtil;
import com.xin.di.uav.common.utils.IdUtils;
import com.xin.di.uav.operate.mapper.UserRoleMapper;
import com.xin.di.uav.operate.po.SysRolePO;
import com.xin.di.uav.operate.po.UserRolePO;
import com.xin.di.uav.user.po.UserPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserAccountServiceImpl implements UserAccountService {
    @Autowired
    private UserAccountMapper userAccountMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public boolean add(UserAccountsAddParam userAccountsAddParam) {
        int count = userAccountMapper.count(userAccountsAddParam.getPhoneNumber(), null, null);
        if (count > 0) {
            throw new BusinessException("手机号已存在");
        }
        UserAccountPO userAccountPO = BeanUtil.copyProperties(userAccountsAddParam, UserAccountPO.class);
        userAccountPO.setId(IdUtils.getId());
        Map<String, Object> info = JwtUtil.getInfo();
        assert info != null;
        userAccountPO.setCreator((String) info.get("displayName"));
        userAccountPO.setCreatorId((String) info.get("userId"));
        userAccountMapper.insert(userAccountPO);

        List<String> roleIds = userAccountsAddParam.getRoleIds();
        if (CollectionUtil.isNotEmpty(roleIds)) {
            for (String roleId : roleIds) {
                UserRolePO userRolePO = new UserRolePO();
                userRolePO.setRoleId(roleId);
                userRolePO.setUserId(userAccountPO.getId());
                userRoleMapper.insert(userRolePO);
            }
        }
        return true;
    }

    @Override
    public boolean delete(String id) {
        return false;
    }

    @Override
    public boolean update(UserAccountsEditParam userAccountsEditParam) {
        UserAccountPO userAccountPO = userAccountMapper.selectById(userAccountsEditParam.getId());
        if (userAccountPO == null) {
            throw new BusinessException("账号不存在");
        }
        userAccountMapper.updateById(userAccountPO);
        return false;
    }

    @Override
    public boolean updateStatus(IdRequest idRequest) {
        return false;
    }

    @Override
    public UserPO getById(String id) {
        return null;
    }

    @Override
    public UserAccountResp list(int pageNum, int pageSize, String phone, Date startTime, Date endTime) {
        UserAccountResp userAccountResp = new UserAccountResp();
        pageNum = (pageNum - 1) * pageSize;
        int count = userAccountMapper.count(phone, startTime, endTime);
        List<UserAccountResp.Record> recordList = new ArrayList<>();
        if (count > 0) {
            userAccountResp.setTotal(count);
            List<UserAccountPO> userAccountPOS = userAccountMapper.selectList(pageNum, pageSize, phone, startTime, endTime);
            List<String> userIds = userAccountPOS.stream().map(UserAccountPO::getId).collect(Collectors.toList());
            List<UserRolePO> userRolePOS = userRoleMapper.selectList(userIds);
            List<SysRolePO> sysRolePOS = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(userRolePOS)) {
                List<String> roleIds = userRolePOS.stream().map(UserRolePO::getRoleId).collect(Collectors.toList());
                sysRolePOS = roleMapper.selRoles(roleIds);

            }
            for (UserAccountPO userAccountPO : userAccountPOS) {
                UserAccountResp.Record record = BeanUtil.copyProperties(userAccountPO, UserAccountResp.Record.class);
                List<String> roleNames = new ArrayList<>();
                for (UserRolePO userRolePO : userRolePOS) {
                    if (record.getId().equals(userRolePO.getUserId())) {
                        for (SysRolePO sysRolePO : sysRolePOS) {
                            if (sysRolePO.getId().equals(userRolePO.getRoleId())) {
                                roleNames.add(sysRolePO.getRoleName());
                            }
                        }
                    }
                }
                record.setRoleNames(roleNames);

                recordList.add(record);
            }
            userAccountResp.setRecordList(recordList);
        }
        return userAccountResp;
    }

    @Override
    public String loginByCode(String userName, String password) {
        return "";
    }
}
